package _022_2_12

func Search(nums []int, target int) int {
	beg := 0
	end := len(nums) - 1
	midIndex := (beg + end) / 2
	targetIndex := -1
	for midIndex < len(nums) && midIndex >= 0 {
		midIndex = (beg + end) / 2

		if target == nums[midIndex] {
			targetIndex = midIndex
			break
		}

		if target < nums[midIndex] {
			end = midIndex - 1
		} else if target > nums[midIndex] {
			beg = midIndex + 1
		}
	}

	return targetIndex
}
